package com.hjm.ui.shopcart;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.shopcart.adapter.ShopCartStoreAdapter;
import com.hjm.ui.R;
import com.hjm.ui.shopcart.entity.ShopCartVO;
import com.hjm.ui.shopcart.entity.Store;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShopCartItem extends LinearLayout{
    private static final String TAG = "ShopCartItem";
    private RecyclerView recyclerView;
    List<ShopCartVO> goodsList;
    List<Store> storeList;
    Map<Integer, List<ShopCartVO>> storeIdMap;
    ShopCartStoreAdapter shopCartStoreAdapter;
    private Listener listener;

    public ShopCartItem(Context context) {
        super(context);
        init();
    }
    public ShopCartItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ShopCartItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
        // 在这里添加你的布局和逻辑
        LayoutInflater.from(getContext()).inflate(R.layout.layout_shop_cart_item, this, true);
        recyclerView = findViewById(R.id.recycler_view_shop_cart_store);
    }
    public <ShopCartVO> void setData(List<com.hjm.ui.shopcart.entity.ShopCartVO> goodsList){
        dataPrepare(goodsList);
        // 一列，8dp间距
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 1) {
            @Override
            public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
                int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
                super.onLayoutChildren(recycler, state);
                for (int i = 0; i < getChildCount(); i++) {
                    View child = getChildAt(i);
                    RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
                    params.setMargins(spacingInPixels, spacingInPixels, spacingInPixels, spacingInPixels);
                    child.setLayoutParams(params);
                }
            }
        });
        Log.d(TAG, "setData: 开始设置数据 "+storeList.size());
        shopCartStoreAdapter = new ShopCartStoreAdapter(storeList, getContext());
        shopCartStoreAdapter.setListener(listener);
        recyclerView.setAdapter(shopCartStoreAdapter);
    }

    /**
     * 数据准备
     */
    void  dataPrepare(List<ShopCartVO> goodsList){
        this.goodsList = goodsList;
        this.storeIdMap = new HashMap<>();
        this.storeList = new ArrayList<>();
        // 根据商店id进行分类
        Log.d(TAG, "dataPrepare: 根据商店id进行分类");
        List<ShopCartVO> newList;
        for (ShopCartVO goods : goodsList) {
            int storeId = goods.getStoreId();
            if (storeIdMap.containsKey(storeId)) {
                storeIdMap.get(storeId).add(goods);
            } else {
                newList  = new ArrayList<>();
                newList.add(goods);
                storeIdMap.put(storeId, newList);
            }
        }
        storeList = storeMapToStoreList(storeIdMap);
    }
    /**
     * 键值对转化为列表
     * @param storeIdMap
     * @return
     */
    List<Store> storeMapToStoreList(Map<Integer, List<ShopCartVO>> storeIdMap){
        // 根据商店id把商品添加到对应的商店列表中
        List<Store> storeList = new ArrayList<>();
        for (Integer storeId : storeIdMap.keySet()) {
            Store store = new Store();
            List<ShopCartVO> shopCartVOList = storeIdMap.get(storeId);
            if (shopCartVOList.size() > 0){
                store.setId(shopCartVOList.get(0).getStoreId());
                store.setName(shopCartVOList.get(0).getStoreName());
                store.setShopCartVOList(shopCartVOList);
                storeList.add(store);
            }
        }
        return storeList;
    }

    public void setListener(Listener listener){
        this.listener = listener;
    }

    public interface Listener{
        /**
         * 某个商品在购物车列表中的数量变化
         * @param orderId
         */
        void onNumChanged(int num,int orderId);

        /**
         * 某个购物车在购物车列表中的选中状态变化
         * @param orderId
         */
        void selectChanged(boolean isSelect, int orderId);

        void selectStoreAllChanged(boolean isSelectAll, int storeId);

        /**
         * 点击某个商品
         * @param goodsId
         */
        void onItemClick(int goodsId);

        void onStoreNameClick(int storeId);
    }
}
